package com.qf.service.impl;/*
 *徐韬
 *2021/10/31
 *19:31
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.limitCache;
import com.qf.constons.resultCode;
import com.qf.events.UpdateLimitEvents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.limitMapper;
import com.qf.pojo.TLimit;
import com.qf.pojo.checkType;
import com.qf.service.limitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class limitServiceimpl implements limitService {
    private limitMapper limitMapper;
    private ApplicationContext context;
    private  limitCache limitCache;
@Autowired
    public void setLimitCache(com.qf.cache.limitCache limitCache) {
        this.limitCache = limitCache;
    }

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setLimitMapper(com.qf.mapper.limitMapper limitMapper) {
        this.limitMapper = limitMapper;
    }

    @Override
    public PageInfo<TLimit> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TLimit> tLimits = limitCache.getfinlAll();
        PageInfo<TLimit> pageInfo = new PageInfo<>(tLimits);
        return pageInfo;
    }

    @Override
    public void addlimit(TLimit tLimit) {
        if(tLimit.isNull(checkType.ADD)){
            throw new addDataException("您传入的数据是空的", resultCode.DATA_ISNULL);
        }
        limitMapper.insert(tLimit);
        context.publishEvent(new UpdateLimitEvents());
    }

    @Override
    public void update(TLimit tLimit) {
        if(tLimit.isNull(checkType.UPDATE)){
            throw new updateException("您传入的参数为null",resultCode.DATA_ISNULL);

        }
        limitMapper.updateLimit(tLimit);
        context.publishEvent(new UpdateLimitEvents());
    }

    @Override
    public void deleteByids(List<Integer> ids) {
        int i = limitMapper.deleteByids(ids);
        context.publishEvent(new UpdateLimitEvents());
    }
}
